import json

from configs import settings
import requests


class APIToken(object):
    def __init__(self):
        self.base_url = settings.LOGIN_URL
        self.headers = {"Content-Type": "application/json"}
        self.data = {
            "value": settings.VALUE,
            "userName": settings.USERNAME,
            "grantType": settings.GRANTTYPE
        }

    def get_token(self):
        """
        获取token值
        :return:token
        """
        requests.packages.urllib3.disable_warnings()
        url = f"{self.base_url}/rest/plat/smapp/v1/oauth/token"
        data = json.dumps(self.data)
        try:
            result = requests.put(url, data=data, headers=self.headers, verify=False,
                                  time_out=3).json()  # 通过verify关闭ssl证书
        except Exception as e:
            print(e)
            return None, None
        else:
            accessSession = result["accessSession"]
            roaRand = result["roaRand"]
            return accessSession, roaRand


if __name__ == '__main__':
    APIToken().get_token()
